package com.hktf.corelib.utils;

import android.app.ActivityManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;

import java.util.List;


/**
 * @包名称： com.yx.corelib.utils
 * @类名： ServiceUtils
 * @创建人： 陈大专
 * @时间： 2020/09/10 10:51
 * @描述：
 */
public class ServiceManager {
    /**
     * 判断某个服务是否在运行
     *
     * @param className
     * @param context
     * @return
     */
    public static boolean isServiceRunning(String className, Context context) {
        //进程管理者
        ActivityManager manager = (ActivityManager) context.getSystemService(context.ACTIVITY_SERVICE);
        //获取进程中正在运行的服务集合
        List<ActivityManager.RunningServiceInfo> runningservice = manager.getRunningServices(1000);
        //遍历
        for (ActivityManager.RunningServiceInfo runningServiceInfo : runningservice) {
            //获取正在运行的服务的标识
            ComponentName service = runningServiceInfo.service;
            //获取正在运行的服务的全类型
            String name = service.getClassName();
            LogMds.i("cdz", "在运行的服务有name=" + name);
            //判断
            if (name.contains(className)) {
                return true;
            }
        }
        return false;
    }

    /**
     * 关闭所有服务
     *
     * @return
     */
    public static void stopAllServices() {
        //进程管理者
        try {
            ActivityManager manager = (ActivityManager) CommUtils.getContext().getSystemService(CommUtils.getContext().ACTIVITY_SERVICE);
            //获取进程中正在运行的服务集合
            List<ActivityManager.RunningServiceInfo> runningservice = manager.getRunningServices(1000);
            //遍历
            for (ActivityManager.RunningServiceInfo runningServiceInfo : runningservice) {
                if (runningServiceInfo.pid!= android.os.Process.myPid()) {
                    continue;
                }
                //获取正在运行的服务的标识
                ComponentName service = runningServiceInfo.service;
                //获取正在运行的服务的全类型
                String name = service.getClassName();
                String packageName = service.getPackageName();
                //判断
                if (packageName.contains("com.yx")) {
                    Intent intent = new Intent();
                    intent.setComponent(service);
                    try {
                        CommUtils.getContext().stopService(intent);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    LogMds.i("cdz", "关闭服务 name=" + name);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
    /**
     * 关闭指定服务
     *
     * @return
     */
    public static void stopService(String className) {
        //进程管理者
        try {
            ActivityManager manager = (ActivityManager) CommUtils.getContext().getSystemService(CommUtils.getContext().ACTIVITY_SERVICE);
            //获取进程中正在运行的服务集合
            List<ActivityManager.RunningServiceInfo> runningservice = manager.getRunningServices(1000);
            //遍历
            for (ActivityManager.RunningServiceInfo runningServiceInfo : runningservice) {
                if (runningServiceInfo.pid!= android.os.Process.myPid()) {
                    continue;
                }
                //获取正在运行的服务的标识
                ComponentName service = runningServiceInfo.service;
                //获取正在运行的服务的全类型
                String name = service.getClassName();
                //判断
                if (name.contains(className)) {
                    Intent intent = new Intent();
                    intent.setComponent(service);
                    CommUtils.getContext().stopService(intent);
                    LogMds.i("cdz", "关闭服务 name=" + name);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
